﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reflection;
using System.Text;

namespace EBA.IoC
{
    /// <summary>
    /// 自动注入的参数
    /// </summary>
    public class AutowiredParameter
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="parameter"></param>
        public AutowiredParameter(ParameterInfo parameter)
        {
            this.Parameter = parameter;

            var import = this.Parameter.GetCustomAttributes(typeof(ImportAttribute), false).OfType<ImportAttribute>().FirstOrDefault();

            if (import != null)
            {
                this.ContractName = import.ContractName;
                this.ContractType = import.ContractType;

                if (this.ContractType == null)
                {
                    this.ContractType = this.Parameter.ParameterType;
                }

                this.IsCollection = false;
            }
            else
            {
                //TODO:not finished yet
                var importMany = this.Parameter.GetCustomAttributes(typeof(ImportManyAttribute), false).OfType<ImportManyAttribute>().FirstOrDefault();

                if (importMany == null)
                {
                    this.ContractType = this.Parameter.ParameterType;
                    this.IsCollection = false;
                }
                else
                {
                    this.ContractName = importMany.ContractName;
                    this.ContractType = importMany.ContractType;
                    
                    this.IsCollection = true;
                }
            }
        }

        /// <summary>
        /// 原始参数信息
        /// </summary>
        public ParameterInfo Parameter { get; internal set; }
        /// <summary>
        /// 要导入的类型信息
        /// </summary>
        public Type ContractType { get; internal set; }
        /// <summary>
        /// 映射的契约名称
        /// </summary>
        public string ContractName { get; internal set; }
        /// <summary>
        /// 是否是数组或者其他集合对象
        /// </summary>
        public bool IsCollection { get; internal set; }
    }
}
